;; -*- coding: utf-8 -*-

(add-to-list 'load-path (expand-file-name "~/.emacs.d/site-lisp/emms"))
(if (string= system-type "windows-nt")
    (add-to-list 'exec-path "C:/mplayer/"))

(require 'emms-setup)
(require 'emms-i18n)
;; auto-save and import last playlist
(require 'emms-history)
(emms-devel)

(setq emms-player-list '(emms-player-mplayer)
      emms-player-mplayer-command-name "mplayer"
      emms-player-mplayer-parameters '("-slave"))

(require 'emms-info-libtag)
(setq emms-info-functions '(emms-info-libtag))

;; mplayer volume settings
;; add follow line to ~/.mplayer/config : af=volume=-25:1
(defun my-mplayer-volume-change-function (amount)
  (interactive)
  (process-send-string
   emms-player-simple-process-name (format "volume %d\n" amount)))

(setq emms-volume-change-function 'my-mplayer-volume-change-function)

;; coding settings
(setq emms-info-mp3info-coding-system 'gbk
      emms-cache-file-coding-system 'gbk
      emms-i18n-default-coding-system '(cp936 . cp936))

(setq emms-playlist-buffer-name "*Music*"
      emms-playing-time-style 'time
      emms-info-asynchronously nil)

(set-face-foreground 'emms-playlist-selected-face "magenta")
;;(set-face-foreground 'emms-playlist-track-face  "green")

(global-set-key (kbd "M-<f3>")
                (lambda ()
                  (interactive)
                  (if emms-playlist-buffer
                      (emms-playlist-mode-go)
                    (emms-history-load))))

(setq emms-repeat-playlist nil)

(add-hook 'emms-playlist-limit-hook
          'emms-playlist-sort-by-natural-order)

(add-hook 'emms-player-finished-hook
          (lambda ()
            (with-current-emms-playlist
              (save-excursion
                (emms-playlist-mode-center-current)
                (emms-playlist-update-track)))))

;; Mode Line Format
(require 'emms-mode-line)
(emms-mode-line 1)

(require 'emms-playing-time)
(emms-playing-time 1)
(emms-playing-time-mode-line)
(setq emms-lyrics-display-on-modeline nil)

(eval-after-load 'emms
  '(progn
     (defun my-emms-track-description-function (track)
       "Return a description of the current track."
       (let* ((name (emms-track-name track))
	      (short-name (file-name-sans-extension
			   (file-name-nondirectory name)))
	      (play-count (or (emms-track-get track 'play-count) 0))
	      (empty "...")
	      (artist (or (emms-track-get track 'info-artist) empty))
	      (title (or (emms-track-get track 'info-title) short-name)))
	 (format " %3d  %-20s%-s" play-count artist title)))
       
     (setq emms-track-description-function 'my-emms-track-description-function)

     ;; Redefine this to use name only
     (defun my-emms-mode-line-playlist-current ()
       "Format the currently playing song."
       (let* ((track (emms-playlist-current-selected-track))
	      (type (emms-track-type track))
	      (name (emms-track-name track))
	      (artist (emms-track-get track 'info-artist))
	      (title (emms-track-get track 'info-title)))
	 (format "[ %s ]"
		 (cond ((and artist title)
			(concat artist " - " title))
		       (title
			title)
		       ((eq type 'file)
			(file-name-sans-extension (file-name-nondirectory name)))
		       (t
			(concat (symbol-name type) ":" name))))))

     (setq emms-mode-line-mode-line-function 'my-emms-mode-line-playlist-current)))

;; (require 'emms-mode-line-icon)
;; (require 'emms-lyrics)
;; (emms-lyrics 1)

;; (setq emms-lyrics-coding-system 'utf-8)
;; (setq emms-lyrics-display-on-minibuffer t) 
;; (setq emms-lyrics-dir (expand-file-name "~/lyrics"))
;; (setq emms-lyrics-scroll-timer-interval 5.0)

;; (require 'emms-lyrics-download)
;; (ad-activate 'emms-lyrics-find-lyric)

(defun emms-playlist-mode-delete-selected-track ()
  "Delete selected(playing) track."
  (interactive)
  (with-current-buffer emms-playlist-buffer
    (emms-playlist-ensure-playlist-buffer)
    (widen)
    (when (y-or-n-p "Delete selected(playing) track ")
      (emms-with-inhibit-read-only-t
       (save-excursion
         (emms-playlist-mode-center-current)
         (let ((track (emms-playlist-current-selected-track))
               (kill-whole-line t))
           (goto-char (line-beginning-position))
           (kill-line)
           (delete-file (emms-track-get track 'name)))
         (emms-playlist-mode-play-smart))))))

;;; Key Bindings
(define-key emms-playlist-mode-map (kbd "x") 'emms-start)
(define-key emms-playlist-mode-map (kbd "v") 'emms-stop)
(define-key emms-playlist-mode-map (kbd "h") 'emms-shuffle)
(define-key emms-playlist-mode-map (kbd "o") 'emms-show)
(define-key emms-playlist-mode-map (kbd "SPC") 'emms-pause)
(define-key emms-playlist-mode-map (kbd "r") 'emms-toggle-repeat-track)
(define-key emms-playlist-mode-map (kbd "R") 'emms-toggle-repeat-playlist)
(define-key emms-playlist-mode-map (kbd "q") 'next-buffer) ; 'delete-window

(define-key emms-playlist-mode-map (kbd "N") 'emms-next)
(define-key emms-playlist-mode-map (kbd "P") 'emms-previous)
(define-key emms-playlist-mode-map (kbd "E") 'emms-tag-editor-edit)

;; emms dired
(define-key emms-playlist-mode-map (kbd "n") 'next-line)
(define-key emms-playlist-mode-map (kbd "p") 'previous-line)
(define-key emms-playlist-mode-map (kbd "d") 'emms-playlist-mode-delete-selected-track)
;; (define-key emms-playlist-mode-map (kbd "D") 'emms-playlist-mode-delete)
;; (define-key emms-playlist-mode-map (kbd "m") 'emms-playlist-mode-mark)
;; (define-key emms-playlist-mode-map (kbd "u") 'emms-playlist-mode-unmark)

(global-set-key (kbd "C-c e x")   'emms-start)
(global-set-key (kbd "C-c e v")   'emms-stop)
(global-set-key (kbd "C-c e n")   'emms-next)
(global-set-key (kbd "C-c e p")   'emms-previous)
(global-set-key (kbd "C-c e o")   'emms-show)
(global-set-key (kbd "C-c e h")   'emms-shuffle)
(global-set-key (kbd "C-c e SPC") 'emms-pause)
(global-set-key (kbd "C-c e f")   'emms-no-next)
(global-set-key (kbd "C-c e F")   'emms-no-next-and-sleep)
(global-set-key (kbd "C-c e t")   'emms-add-dired)
(global-set-key (kbd "C-c e d")   'emms-playlist-mode-delete-selected-track)
(global-set-key (kbd "C-c e r")   'emms-toggle-repeat-track)
(global-set-key (kbd "C-c e R")   'emms-toggle-repeat-playlist)
(global-set-key (kbd "C-c e l")   'emms-lyrics-visit-lyric)

(provide 'init-emms)
